package com.terren.spider.core.html.scheme.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.terren.spider.entity.common.PageData;
import com.terren.spider.entity.core.Entry;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.utils.UrlUtils;

public class LJChengjiaofangScheme extends BasicCustomScheme{


	@Override
	public Set<String> findHelpPaginationUrl(Page page, Entry entry) {
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(findPagination(page, entry)); // 分页部分
		return resultSet;
	}

	/**
	 * 分页+条件筛选（匹配带有房源类型和楼层类型的列表页url）
	 * @param page
	 * @param entry
	 * @return
	 */
	private Set<String> findPagination(Page page, Entry entry) {
		//List<String> areas = page.getHtml().xpath("//div[3]/div[1]/dl[2]/dd/div/div/a").links().all();//区
		//List<String> lareas = page.getHtml().xpath("//div[3]/div[1]/dl[2]/dd/div/div[2]").links().all();//乡镇
		String houseSrc[] = {"l1","l2","l3","l4","l5"};//房源类型 
		String houseLc[] = {"lc1","lc2","lc3","lc4","lc5"};//楼层类型
		List<String> combinList = new ArrayList<String>();
		List<String> mixList = new ArrayList<String>();
		//房源楼层拼接
		for(String strsrc: houseSrc){
			for(String strlc: houseLc){
				combinList.add(strsrc+strlc+"/");
			}
		}
		//List<String> newAreaList = new ArrayList<String>();
		//去掉lc1l1/ 等
		
		String str1 = page.getUrl().get().substring(0, page.getUrl().get().lastIndexOf("/"));
		String str2 = str1.substring(0, str1.lastIndexOf("/"));
		
		/*}*/
		//入口url与combinList结合
		//String entryUrl = StringUtils.substringBefore(entry.getEntryUrl(), "lc1l1/");//截取要替换的部分
		/*for(String areaUrl:newAreaList){*/
		for(String url:combinList){
			mixList.add(str2+"/"+url);//拼接完整的详情页url
		}
		/*}*/
	
		List<String> pgList = new ArrayList<String>();// 存放分页list
		String pageUrl = "";
		String pageDataStr = "";
		List<String> pageUrls = page.getHtml().$("div.page-box", "page-url").all();
		List<String> pageDataStrs = page.getHtml().$("div.page-box", "page-data").all();
		if (pageUrls != null && pageUrls.size() >= 2) {
			pageUrl = pageUrls.get(1);
		}
		if (pageDataStrs != null && pageDataStrs.size() >= 2) {
			pageDataStr = pageDataStrs.get(1);
		}
		if (!"".equals(pageDataStr) && !"".equals(pageUrl)) {
			PageData pageData = new PageData();
			Gson g = new Gson();
			List<PageData> list = g.fromJson("[" + pageDataStr + "]", new TypeToken<List<PageData>>() {
			}.getType());
			pageData = list.get(0);
			//String areaParam = str1.substring(8,10);
			String domain =UrlUtils.getDomain(entry.getEntryUrl());
			for (int i = 2; i <= pageData.getTotalPage(); i++) {
				pgList.add("https://"+domain + pageUrl.replace("{page}", i + ""));
			}
		}
		Set<String> resultSet = new HashSet<>();
		//resultSet.addAll(areas);
		resultSet.addAll(mixList);
		resultSet.addAll(pgList);
		resultSet.remove(null);
		return resultSet;
	}
}
